package com.zx.BookSeaReader.ui.login;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import android.widget.TextView;
import android.widget.Toast;


import com.zx.BookSeaReader.R;

import com.zx.BookSeaReader.common.APPCONST;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.service.UserService;
import com.zx.BookSeaReader.ui.home.MainActivity;
import com.zx.BookSeaReader.util.TextHelper;


public class LoginActivity extends AppCompatActivity{
    @BindView(R.id.et_loginactivity_username)
    EditText mEtLoginUsername;
    @BindView(R.id.et_loginactivity_password)
    EditText mEtLoginPassword;
    @BindView(R.id.tv_loginactivity_register)
    TextView mTvLoginRegister;
    @BindView(R.id.checkBoxLogin)
    CheckBox mCbLogin;
    @BindView(R.id.bt_loginactivity_login)
    Button mBtLogin;

    private String name;
    private String password;

    private UserService mUserService;
    private boolean match = false;
    SharedPreferences sp = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mUserService=new UserService();

        UserBean userBean = new UserBean();
        userBean.setPassword("123456");
        userBean.setUsername("admin");
        userBean.setAdmin(1);
        boolean check =mUserService.existUser("admin");
        if(!check){
            mUserService.addUserBean(userBean);
        }


        if (sp.getBoolean("checkboxBoolean", false))
        {
            mEtLoginUsername.setText(sp.getString("mEtLoginUsername", null));
            mEtLoginPassword.setText(sp.getString("mEtLoginPassword", null));
            mCbLogin.setChecked(true);

        }
        initClick();

    }
    protected void initClick() {

        mTvLoginRegister.setOnClickListener(
                (v) ->{
                    startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                    finish();
                }
        );
        mBtLogin.setOnClickListener(
                (v) ->checkUser()
        );
    }

    private void checkUser() {
        name = mEtLoginUsername.getText().toString().trim();
        password = mEtLoginPassword.getText().toString().trim();
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {

            match =mUserService.existUser(name);
            if (match) {
                boolean check = mUserService.checkUser(name,password);
                if(check) {
                    boolean CheckBoxLogin = mCbLogin.isChecked();
                    if (CheckBoxLogin) {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("mEtLoginUsername", name);
                        editor.putString("mEtLoginPassword", password);
                        editor.putBoolean("checkboxBoolean", true);
                        editor.commit();
                    } else {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("mEtLoginUsername", null);
                        editor.putString("mEtLoginPassword", null);
                        editor.putBoolean("checkboxBoolean", false);
                        editor.commit();
                    }
                    UserBean user = mUserService.getUser(name);

                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra(APPCONST.USER, user);
                    startActivity(intent);
                    finish();
                }else{
                    TextHelper.showText("密码错误，请重新输入");
                }
            } else {
                TextHelper.showText("用户名或密码不正确，请重新输入");

            }
        } else {
            TextHelper.showText("请输入你的用户名或密码");
        }

    }



}
